package com.cskaoyan.service;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.data.ResCommentListData;

import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class CommentServiceImpl implements CommentService {
    @Override
    public CommonData<MarketComment> list(ResCommentListData data) {
        TransactionUtil.startTransaction();
        //开启分页
        Integer page = data.getPage();
        Integer limit = data.getLimit();
        PageHelper.startPage(page, limit);
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketCommentMapper commentMapper = sqlSession.getMapper(MarketCommentMapper.class);
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentCriteria = commentExample.createCriteria();
        commentCriteria.andDeletedEqualTo(false);

        if(data.getUserId()!=null){
            commentCriteria.andUserIdEqualTo(data.getUserId());
        }
        if(data.getValueId()!=null){
            commentCriteria.andValueIdEqualTo(data.getValueId());
        }
        List<MarketComment> commentList = commentMapper.selectByExample(commentExample);
        //封装数据
        sqlSession.close();


        return CommonData.data(new PageInfo(commentList));
    }

    @Override
    public void delete(MarketComment comment) {
        TransactionUtil.startTransaction();
        Integer id = comment.getId();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketCommentMapper commentMapper = sqlSession.getMapper(MarketCommentMapper.class);
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentCriteria = commentExample.createCriteria();
        commentCriteria.andIdEqualTo(id);
        //通过id获取该条评论
        MarketComment record = commentMapper.selectByExample(commentExample).get(0);
        record.setDeleted(true);
        Integer i = commentMapper.updateByExample(record, commentExample);
        System.out.println("删除了"+i+"条数据");
        sqlSession.commit();
        sqlSession.close();
    }
}
